package com.edu.study.base.stuLevel.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.edu.study.base.stuLevel.domain.StuLevel;
import com.edu.study.base.stuLevel.service.IStuLevelService;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * stuLevelController
 * 
 * @author zw
 * @date 2021-05-28
 */
@RestController
@RequestMapping("/stuLevel/stuLevel")
public class StuLevelController extends BaseController
{
    @Autowired
    private IStuLevelService stuLevelService;

    /**
     * 查询stuLevel列表
     */
    @PreAuthorize("@ss.hasPermi('stuLevel:stuLevel:list')")
    @GetMapping("/list")
    public AjaxResult list(StuLevel stuLevel)
    {
        List<StuLevel> list = stuLevelService.selectStuLevelList(stuLevel);
        return AjaxResult.success(list);
    }

    @GetMapping("/treeDataInfo")
    public AjaxResult treeDataInfo(){
        List<StuLevel> list = stuLevelService.selectStuLevelList(null);
        List<StuLevel> parentList = list.stream().filter(e -> e.getParentId().longValue() == 0).collect(Collectors.toList());
        if (parentList != null && parentList.size() > 0) {
            for (StuLevel stuLevel : parentList) {
                stuLevel.setChildren(getChildrenData(stuLevel,list));
            }
        }
        return AjaxResult.success(parentList);
    }

    private List<StuLevel> getChildrenData(StuLevel stuLevel ,List<StuLevel> list){
        List<StuLevel> parentList = new ArrayList<>();
        if(stuLevel != null){
            parentList = list.stream().filter(e -> e.getParentId().longValue() == stuLevel.getId().longValue()).collect(Collectors.toList());
            if(parentList != null && parentList.size() > 0){
                for (StuLevel level : parentList) {
                    level.setChildren(getChildrenData(level,list));
                }
            }
        }
        return parentList;
    }
    /**
     * 导出stuLevel列表
     */
    @PreAuthorize("@ss.hasPermi('stuLevel:stuLevel:export')")
    @Log(title = "stuLevel", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(StuLevel stuLevel)
    {
        List<StuLevel> list = stuLevelService.selectStuLevelList(stuLevel);
        ExcelUtil<StuLevel> util = new ExcelUtil<StuLevel>(StuLevel.class);
        return util.exportExcel(list, "stuLevel");
    }

    /**
     * 获取stuLevel详细信息
     */
    @PreAuthorize("@ss.hasPermi('stuLevel:stuLevel:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(stuLevelService.selectStuLevelById(id));
    }

    /**
     * 新增stuLevel
     */
    @PreAuthorize("@ss.hasPermi('stuLevel:stuLevel:add')")
    @Log(title = "stuLevel", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody StuLevel stuLevel)
    {
        if(StringUtils.isEmpty(stuLevel.getLevelNum())){
            stuLevel.setLevelNum(IdUtils.fastSimpleUUID());
        }
        return toAjax(stuLevelService.insertStuLevel(stuLevel));
    }

    /**
     * 修改stuLevel
     */
    @PreAuthorize("@ss.hasPermi('stuLevel:stuLevel:edit')")
    @Log(title = "stuLevel", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody StuLevel stuLevel)
    {
        return toAjax(stuLevelService.updateStuLevel(stuLevel));
    }

    /**
     * 删除stuLevel
     */
    @PreAuthorize("@ss.hasPermi('stuLevel:stuLevel:remove')")
    @Log(title = "stuLevel", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(stuLevelService.deleteStuLevelByIds(ids));
    }



}
